// pages/goodsDetail/goodsDetail.js

const app = getApp()
const util = require("../../utils/util.js")

const WxParse = require('../../wxParse/wxParse.js');

Page({
  /**
   * 页面的初始数据
   */
  data: {
    goodsId:1181000,
    attribute: [],
    brand: [],
    // 评论
    comment: [],
    // 评论列表图片
    pic_list:[],
    gallery: [],
    // info
    goodsList: [],
    issue: [],
    productList: [],
    specificationList: [],
    // userHasCollect: 0,
    isCollect:false,
    num:1,
    // 是否显示遮照
    isMask:false,
    relatedList:[],
    // filter
    arrValue:[],
    newText:[],
    newNewText:'请输入商品属性',
    newSpecId:0,
    productId:1,
  },
  // 查看更多评论
  toComment(e){
    console.log(e);
    wx.navigateTo({
      url:'/pages/comment/comment'
    })
  },
  // 添加商品
  addNum(){
    let that = this;
    let num = that.data.num;
    num ++;
    that.setData({
      num:num
    })
  },
  // 删除商品
  delNum(){
    let that = this;
    let num = that.data.num;
    if(num<=1){
      num == 0
    }else{
      num --;
      that.setData({
        num:num
      })
    }
  },
   // 回到首页
  goHome(e){
    // console.log(e);
    wx.switchTab({
      url:"../index/index"
    })
  },
  // 跳转到购物车
  goCart(e){
    wx.switchTab({
      url:"../cart/cart"
    })
  },
  // 收藏
  collect(e){
    let token = wx.getStorageSync('token');
    if(!!token){
      let productId = e.currentTarget.dataset.id;
      let bool = !this.data.isCollect;
      if(bool == false){
        bool = true;
        wx.showToast({
          title:'取消收藏'
        })
        util.addOrDeleteCollect(productId);
      }else{
        bool = false;
        wx.showToast({
          title:'收藏成功'
        })
        util.addOrDeleteCollect(productId);
        // 如何保存收藏的状态，使得下次进入的时候，还说收藏或取消收藏？
      }
      bool = !bool;
      this.setData({
        isCollect: bool
      })
    } else {
      wx.navigateTo({
        url: "/pages/login/login"
      })
    }

  },
  // 添加购物车
  addCart(e){
    let token = wx.getStorageSync('token');
    if(!!token){
      let goodsId = e.currentTarget.dataset.goodsid;
      console.log(goodsId);
      let flag = this.data.isMask;
      if(!!flag){
        flag = true;
      }else{
        flag = false;
      }
      flag = !flag;
      this.setData({
        isMask:flag,    // 显示遮照
      })
    } else {
      wx.navigateTo({
        url: "/pages/login/login"
      })
    }

  },
  getNewArr:function(){
    // console.log("gatNewArr start")
    let list = this.data.specificationList;
    let str = [];
    let arr = [];
    list.forEach((item) => {
      arr.push(item.valueList[0].id);
      str.push(item.valueList[0].value);
    })
    let numId = arr[0]+"_"+arr[1];
    this.setData({
      arrValue: arr,
      newText:str,
      newNewText: str.join(" "),
      newSpecId:numId,
    });
  },
  getItm(e){
    console.log(e);
    let itm = e.currentTarget.dataset.itm;
    let id = itm.id;
    let value = itm.value;
    let index = itm.specification_id -1;
    let arr = this.data.arrValue;
    let str = this.data.newText;
    arr[index] = id;
    str[index] = value;
    let numId = arr[0]+"_"+arr[1];
    this.setData({
      arrValue: arr,
      newText: str,
      newNewText:str.join(" "),
      newSpecId:numId,
    })
  },
  getRelatedID(e){
    let relatedId = e.currentTarget.dataset.relatedid;
    wx.redirectTo({
      url: './goodsDetail?id='+relatedId
    })
  },
  maskParent(){
    console.log("parent");
    let flag = this.data.isMask;
    console.log(flag);
    flag = false;
    this.setData({
      isMask:flag
    })
  },
  maskChildren(){
    console.log("children");
  },
  toCart(){
    let newSpecId = this.data.newSpecId;
    let productList = this.data.productList;
    let specificationList = this.data.specificationList;
    let id = 1;
    if(specificationList.length <= 0){
      id = productList[0].id
    }else{
      productList.forEach(item => {
        if(newSpecId == item.goods_specification_ids){
          id = item.id
        }
      })
    }
    console.log(id);
    this.setData({
      productId:id
    })
  },
  toSure(){
    this.toCart();
    let goodsId = parseInt(this.data.goodsId);
    let num = this.data.num;
    let productId = this.data.productId;
    util.addGoods(goodsId,num,productId);
    console.log(goodsId,num,productId);
    let isMask = this.data.isMask;
    isMask = false;
    this.setData({
      isMask:isMask
    })
    wx.showToast({
      title:'添加成功'
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    console.log('onLoad' , options );
    try {
      JSON.parse(options.id)
      this.setData({
        goodsId:options.id
      })
      util.myRequest({
        url: util.api.goodsDetail,
        methods: 'get',
        data: {
          id: options.id,
          // id:this.data.goodsId
        },
        success: function (res) {
          console.log(res)
          that.setData({
            attribute:    res.data.attribute,
            brand:        res.data.brand,
            comment:      res.data.comment,
            goodsList:    res.data.info,
            picList:      res.data.comment.data.pic_list,
            issue:        res.data.issue,
            gallery:      res.data.gallery,
            specificationList:  res.data.specificationList,
            productList:  res.data.productList,
            retailPrice:  res.data.info.retail_price,
            isCollect:    res.data.userHasCollect
          })
          let ceshi = res.data.info.goods_desc;
          WxParse.wxParse('article', 'html', ceshi, that, 5); 
          that.getNewArr();
        }
      })
      // 请求相关商品
      util.myRequest({
        url:util.api.goodsRelated,
        data:{
          id:options.id
        },
        success:function(res){
          let relatedList = res.data.goodsList;
          // console.log("goodsRelated",relatedList);
          that.setData({
            relatedList:relatedList
          })
        }
      })
    } catch (err) {
      console.log(err)
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
})